package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.jicai.view.fragment.HomeJiCaiQiYeFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.List;

/**
 * 集采首页企业专项列表
 */
public class HomeJiCaiQiYeModel extends ViewModel {

    private HomeJiCaiQiYeFragment homeJiCaiQiYeFragment;
    private int page = 1;


    //首页只显示8条数据，无需传参
    //首页只显示8条数据，无需传参
    public void getJiCaiList(JiCaiHomeListRequestBean map,boolean loadMore) {
        page = loadMore ? page + 1 : 1;
        map.setLimit(8);
        map.setCurrent(page);
        map.setGroupBuyMode("2");
        DialogHelper.showProgressDialog(homeJiCaiQiYeFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getJiCaiLists(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CentralizedMiningEntity>>> data) {
                homeJiCaiQiYeFragment.showList(data.getData().getList(),loadMore);
            }
        });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(homeJiCaiQiYeFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("collection").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(homeJiCaiQiYeFragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = data.getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + token;
                    homeJiCaiQiYeFragment.showUrl(url);
                } else {
                    MToast.showToast(homeJiCaiQiYeFragment.getContext(), data.getMsg());
                }
            }
        });
    }

    // 集采分类
    public void groupBuyClassList() {
        JiCaiBaseApi.getApi().groupBuyClassList().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    homeJiCaiQiYeFragment.setTypeDatas(data.getData());
                }
            }
        });
    }

    //获取二级信息
    public void getTypeDatesList(String groupBuyType) {
        DialogHelper.showProgressDialog(homeJiCaiQiYeFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getSecondCategorys("0", groupBuyType).enqueue(new ICallBack<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MenuEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)){
                    if (data.getData()!=null&&data.getData().size()>0){
                        homeJiCaiQiYeFragment.setTypesFirstDatas(data.getData());
                    }else {
                        MToast.showToast(homeJiCaiQiYeFragment.getContext(),"暂无数据信息！");
                    }

                }
            }
        });
    }

    public void setActivity(HomeJiCaiQiYeFragment homeJiCaiQiYeFragment) {
        this.homeJiCaiQiYeFragment = homeJiCaiQiYeFragment;
    }
}
